<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>封装函数返回元素的下标</title>
</head>

<body>
  <script>
    // 封装函数返回元素的下标  [1, 5, 10, 22, 8, 7]

    function getIndex(ele, arr = []) {
      // 1. 定义index
      let index = -1
      // 2. 遍历数组
      for (let i = 0; i < arr.length; i++) {
        // 2.1 判断
        if (ele === arr[i]) {
          // 2.2 返回当前元素对应的下标
          index = i
          // 2.3 结束循环
          break
        }
      }
      // 3. 返回结果
      return index
    }

    let res1 = getIndex(888, [1, 3, 9, 10, 88, 77])
    console.log(res1)
  </script>
</body>

</html>